package com.ming.redis.controller;

import com.alibaba.fastjson.JSONObject;
import com.ming.redis.model.TestPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Auther: tangC
 * @Date: 2020/7/6 11:23
 * @Description:
 */
@RestController
public class TestController {

    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping(value = "test")
    public String test(String message){
        redisTemplate.opsForList().leftPush("test",new Long(new Date().getTime()).toString());
        return "test";
    }
    @RequestMapping(value = "test2")
    public String test2(String message){
        redisTemplate.convertAndSend("test2",message);
        return "test";
    }
    @RequestMapping(value = "test3")
    public String test3(){
        List<TestPojo> testPojos = new ArrayList<>();
        TestPojo testPojo = new TestPojo();
        testPojo.setA("aaa");
        testPojo.setB("bbb");
        testPojos.add(testPojo);
        TestPojo b = new TestPojo();
        b.setA("1");
        b.setB("2");
        testPojos.add(b);
        redisTemplate.convertAndSend("test3", JSONObject.toJSONString(testPojos));
        System.out.println(JSONObject.toJSONString(testPojos));
        return "test";
    }
}
